﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Data;
using System.Data.Odbc;
using WLMCoreLibrary;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Serialization;

namespace MiWebSite_Core
{
    [Serializable]
    public class Categoria : BaseClass
    {
        
        


        #region public properties
        //public String idImagen {get;set; }
        [XmlIgnore()]
        public int idPadre {get;set; }
        [XmlAttribute("order")]
        public int orden {get;set; }
        [XmlElement("caption")]
        public String nombre {get;set; }
        [XmlIgnore()]
        public int idUsuario {get;set; }
        [XmlAttribute("link")]
        public String URLLink {get;set; }
        [XmlElement ("item")]
        public List<Categoria> Items
        {
           get;set;
          
        }
        [XmlAttribute("systemName")]
        public String SystemName {get;set; }
        #endregion

        #region constructors
        public Categoria()
        {
        }

        public Categoria(bool CreateRegistry, ref dbConnector ConexionDB)
        {
            
            if (CreateRegistry)
            {
                ConexionDBInterna = ConexionDB;
                WLMClassBinder classBinder = new WLMClassBinder(ref ConexionDB);
                id = classBinder.NewRecord("categorias");
                string queryString = "";
                queryString = "SELECT * FROM categorias WHERE id='" + id + "'";
                GetRecord(queryString);
            }
        }

        public Categoria(int registryId, ref dbConnector ConexionDB)
        {
            ConexionDBInterna = ConexionDB;
            string queryString = "";
            queryString = "SELECT * FROM categorias WHERE id='" + registryId + "'";
            GetRecord(queryString);        
        }

        public Categoria(int registryId,string parentPath, ref dbConnector ConexionDB)
        {
            ConexionDBInterna = ConexionDB;
            string queryString = "";
            queryString = "SELECT * FROM categorias WHERE id='" + registryId + "'";
            GetRecord(queryString);
            string thisPath = parentPath + "/" + SystemName;
            if (id <=10)
                thisPath = parentPath;
            if (URLLink == null || URLLink == "")
            {
                URLLink = thisPath;
            }
            //URLLink = "prueba";
            Items = new NavigationCollection(id, thisPath,ref ConexionDB).Items;
            
        }

        public Categoria(string systemName, ref dbConnector ConexionDB)
        {
            ConexionDBInterna = ConexionDB;
            string queryString = "";
            queryString = "SELECT * FROM categorias WHERE systemName='" + systemName + "'";
            GetRecord(queryString);
        }

        public Categoria(string nombreOpcion, string URLOpcion, int OrdenOpcion)
        {
            orden = OrdenOpcion;
            nombre = nombreOpcion;
            URLLink = URLOpcion;
        }
        #endregion

        #region public methods


        public void Delete()
        {
            string queryString = "";
            queryString = "DELETE FROM categorias WHERE id=\"" + id + "\"";
            ConexionDBInterna.ExecuteCommand(queryString);

        }     
        
        #endregion

        #region Private Methods
        private void GetRecord(string queryString)
        {
            OdbcDataReader reader = ConexionDBInterna.ExecuteReader(queryString);
            if (reader.HasRows)
            {
                if (!reader.IsDBNull(reader.GetOrdinal("id")))
                    id = (System.Int32)reader["id"];
                if (!reader.IsDBNull(reader.GetOrdinal("fechaCreacion")))
                    fechaCreacion = Convert.ToDateTime(reader["fechaCreacion"]); if (!reader.IsDBNull(reader.GetOrdinal("fechaModificacion"))) fechaModificacion = Convert.ToDateTime(reader["fechaModificacion"]); if (!reader.IsDBNull(reader.GetOrdinal("idUsuarioModificacion"))) idUsuarioModificacion = (System.Int32)reader["idUsuarioModificacion"];


                if (!reader.IsDBNull(reader.GetOrdinal("idPadre")))
                    idPadre = (System.Int32)reader["idPadre"];
                if (!reader.IsDBNull(reader.GetOrdinal("orden")))
                    orden = (System.Int32)reader["orden"];
                if (!reader.IsDBNull(reader.GetOrdinal("nombre")))
                    nombre = (System.String)reader["nombre"];
                if (!reader.IsDBNull(reader.GetOrdinal("idUsuario")))
                    idUsuario = (System.Int32)reader["idUsuario"];
                if (!reader.IsDBNull(reader.GetOrdinal("url")))
                    URLLink = (System.String)reader["url"];
                if (!reader.IsDBNull(reader.GetOrdinal("systemName")))
                    SystemName = (System.String)reader["systemName"];
            }
        }


        protected override void UpdateBind()
        {
            StringBuilder strCommand = new StringBuilder();
            strCommand.Append("UPDATE categorias set "); strCommand.Append("fechaModificacion='" + this.fechaModificacion.ToString("yyyy-MM-dd HH:mm:ss") + "'");
            strCommand.Append(",idUsuarioModificacion=\"" + idUsuarioModificacion.ToString() + "\"");
            strCommand.Append(",idPadre=\"" + idPadre.ToString() + "\"");
            strCommand.Append(",orden=\"" + orden.ToString() + "\"");
            strCommand.Append(",nombre=\"" + nombre.ToString() + "\"");
            strCommand.Append(",url=\"" + URLLink.ToString() + "\"");
            strCommand.Append(",systemName=\"" + SystemName.ToString() + "\"");
            strCommand.Append(" WHERE id=\"" + id + "\"");
            ConexionDBInterna.ExecuteCommand(strCommand.ToString());
        }
        #endregion

    }


    public class NavigationCollection
    {
        public List<Categoria> Items { get; set; }

        public NavigationCollection()
        {
        }
        public NavigationCollection(int idParentCategory,string parentPath, ref dbConnector ConexionDB)
        {

            string strCommand = "SELECT id FROM categorias where idPadre='" + idParentCategory + "' order by ORDEN";

            OdbcDataReader reader = ConexionDB.ExecuteReader(strCommand);

            Items = new List<Categoria>();
            while (reader.Read())
            {
                Categoria newEntry = new Categoria(Convert.ToInt32(reader["id"]),parentPath,ref ConexionDB);
                Items.Add(newEntry);
            }
            reader.Close();
            reader.Dispose();
        }
    }

}
